package com.maxd.respository;

import com.maxd.model.SequenceInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

public interface SequenceInfoRepository extends JpaRepository<SequenceInfo, String>, JpaSpecificationExecutor<SequenceInfo> {

    @Transactional
    @Modifying
    @Query(value = "insert into sequence_info (`scene`,`current_value`, `step`) values (:scene, :currentValue, :step)",nativeQuery = true)
    void insert(@Param("scene") String scene, @Param("currentValue") Long currentValue, @Param("step") Long step);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update sequence_info set current_value = current_value + :step, step = :step where scene = :scene and current_value = :current",nativeQuery = true)
    int increase(@Param("scene") String scene, @Param("current") Long current, @Param("step") Long step);

}